﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace POB
{
    class CircleMenu
    {
        public List<IMenuItem> Items
        {
            get { return AllItems.Peek(); }
            set
            {
                if(AllItems.Count > 0)
                    AllItems.Pop();
                AllItems.Push(value);
            }
        }

        public bool Active { get; set; }

        public int Dept { get { return AllItems.Count; } }

        private Stack<List<IMenuItem>> AllItems;

        int pointingAt = -1;

        public CircleMenu()
        {
            AllItems = new Stack<List<IMenuItem>>();
        }

        public void NavigateToSubMenu(List<IMenuItem> newItems)
        {
            AllItems.Push(newItems);
        }

        public void NavigateBack()
        {
            if(AllItems.Count >= 1)
            {
                AllItems.Pop();
            }
        }

        internal void NavigateBack(bool toStart)
        {
            if(toStart)
            {
                for(int i = 0; i < AllItems.Count - 1 ; i++)
                {
                    AllItems.Pop();
                }
            }
            else
            {
                NavigateBack();
            }
        }

        public void Input(InputState input, PlayerIndex playerIndex)
        {
            if(Active)
            {
                Vector2 stick = input.CurrentGamePadStates[(int)playerIndex].ThumbSticks.Left;

                if(stick.Length() < 0.5f)
                {
                    pointingAt = -1;
                    
                    Items[0].NothingSelected(this, input, playerIndex);
                }
                else
                {
                    float direction = (float)Math.Atan2(stick.Y, -stick.X);
                    direction += (MathHelper.TwoPi / Items.Count) / 2;
                    direction = MathHelper.WrapAngle(direction) + MathHelper.Pi;
                    pointingAt = (int)(direction / (MathHelper.TwoPi / Items.Count));
                    if(pointingAt >= Items.Count)
                        pointingAt = 0;

                    Items[pointingAt].Selected(this, input, playerIndex);
                }
            }
        }

        public void Draw(SpriteBatch batch, Matrix matrix, float radius)
        {
            if(Active)
            {
                for(int i = 0; i < Items.Count; i++)
                {
                    //matrix *= Matrix.CreateRotationZ(-MathHelper.Pi); //So we start at the Left
                    float rotation = i * MathHelper.TwoPi / Items.Count;
                    Matrix finalRotation = Matrix.CreateTranslation(radius, 0f, 0f) * Matrix.CreateRotationZ(rotation);
                    Matrix rotatedMatrix = finalRotation * matrix;
                    Matrix horizontalMatrix = Matrix.CreateRotationZ(-rotation) * finalRotation * matrix;
                    Items[i].DrawMenuIcon(batch, horizontalMatrix, rotatedMatrix, i == pointingAt);
                }

                if(pointingAt >= 0)
                {
                    Items[pointingAt].DrawIfSelected(batch, matrix);
                }
                else
                {
                    Items[0].DrawNothingSelected(batch, matrix);
                }
            }
        }
    }

    interface IMenuItem
    {
        void DrawMenuIcon(SpriteBatch spriteBatch, Matrix horizontalMatrix, Matrix rotatedMatrix, bool selected);
        void DrawIfSelected(SpriteBatch spriteBatch, Matrix matrix);
        void Selected(CircleMenu parentMenu, InputState input, PlayerIndex playerIndex);
        void DrawNothingSelected(SpriteBatch spriteBatch, Matrix matrix);
        void NothingSelected(CircleMenu parentMenu, InputState input, PlayerIndex playerIndex);
    }
}
